package com.shajj.data;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/plain;charset=utf-8");
		String type = req.getParameter("type");
		if ("page".equals(type)) {
			int start = Integer.parseInt(req.getParameter("start"));
			int size = Integer.parseInt(req.getParameter("size"));
			List<Message> list = DataManager.getInstance().getByPage(start, size);
			StringBuilder sb = new StringBuilder();;
			if (list != null) {
				for (Message message : list) {
					sb.append(message.getContent().getValue()).append("||");
				}
			}
			resp.getWriter().write(sb.toString());
		} else {
			Long id = Long.parseLong(req.getParameter("id"));
			Message message = DataManager.getInstance().get(id);
			if (message != null) {
				resp.getWriter().write(message.getContent().getValue());
			} else {
				resp.getWriter().write("不存在");
			}
		}
	}

}
